package com.example.allshengmedical.experimentrun.activity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.R.integer;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.allsheng.widget.AllShengDialog;
import com.allsheng.widget.AllShengDialog.ClickListenerInterface;
import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.base.BaseApplication;
import com.example.allshengmedical.experimentanalyze.activity.AnalyzeAmplificationplotActivity;
import com.example.allshengmedical.experimentanalyze.activity.AnalyzeBrowseActivity;
import com.example.allshengmedical.experimentnew.bean.LayoutTableBean;
import com.example.allshengmedical.experimentnew.bean.MethodBean;
import com.example.allshengmedical.experimentrun.presenter.TempareturePresenter;
import com.example.allshengmedical.experimentrun.view.ITemparetureView;
import com.example.allshengmedical.mainmenu.activity.MainMenuActivity;
import com.example.allshengmedical.util.ByAlert;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;


//运行的温度界面
public class TemparetureActivity extends BaseActivity implements ITemparetureView, OnClickListener {

	public final int TASK_LOOP_COMPLETE = 0;
	ProgressDialog pd;
	private TempareturePresenter tempareturePresenter;

	private LineChart chart;
	private TextView temparetureTv1, temparetureTv2;
	private boolean tempareture1 = true;
	private boolean tempareture2 = true;

	private ProgressBar progressBar;

	// 基因盘数据
	public List<HashMap<String, Object>> dataSourceListBlock1 = new ArrayList<HashMap<String, Object>>();
	public List<HashMap<String, Object>> dataSourceListBlock2 = new ArrayList<HashMap<String, Object>>();
	public List<HashMap<String, Object>> dataSourceListBlockToShow = new ArrayList<HashMap<String, Object>>();
	private ArrayList<LayoutTableBean> activityLayoutTableBeans = new ArrayList<LayoutTableBean>();
	// 曲线
	private LineDataSet dataSet1;
	private LineDataSet dataSet2;
	private ArrayList<LineDataSet> dataSets;
	private ArrayList<String> xValues;
	ArrayList<Entry> yValue = new ArrayList<Entry>();
	// int i = 0;
	ArrayList<Entry> yValue1 = new ArrayList<Entry>();
	// Intent serviceintent;
	// private boolean isExperimentFinish = false;
	private RunMethodService myservice = null;
	Button leftfirstBtn;
	Button leftsecondBtn, leftThird_btn, leftForth_btn, leftFifth_btn, leftSixth_btn, leftSevenTh_btn, leftEighth_btn,
			leftNineth_btn, rightfirst_btn, leftTenth_btn;
	LinearLayout rightline;
	TempDBHelper dbHelper;
	ArrayList<String> tempArrayList = new ArrayList<String>();
	ArrayList<String> temp2ArrayList = new ArrayList<String>();
	ArrayList<String> temp3ArrayList = new ArrayList<String>();
	TextView start_time, end_time, nowTempMod1, nowTempMod2, nowTempMod3, time_count, phototime_count;
	LinearLayout max_lin;
	boolean isStopService = false;
	boolean isFirstFinish = true;

	// PopupWindow popWindow;

	@Override
	protected int setcontent() {
		return R.layout.activity_run_temperatureplot;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void initView() {
		
		tempareturePresenter = new TempareturePresenter(this);
		pd = ProgressDialog.show(TemparetureActivity.this, "", "loading...", false, false);
		new TestTask().execute();
		
		
//		dbHelper = new TempDBHelper(TemparetureActivity.this);
//		tempArrayList = dbHelper.query();
//		// temp2ArrayList = dbHelper.queryTempMode2Name(db);
//		temp3ArrayList = dbHelper.queryTempMode3Name();
//		Intent intent = getIntent();
//		dataSourceListBlock1 = (List<HashMap<String, Object>>) intent.getSerializableExtra("dataSourceListBlock1");
//		dataSourceListBlock2 = (List<HashMap<String, Object>>) intent.getSerializableExtra("dataSourceListBlock2");
//
//		activityLayoutTableBeans = (ArrayList<LayoutTableBean>) intent.getSerializableExtra("activityLayoutTableBeans");
//		IntentFilter finishIntentFilter = new IntentFilter("ExperimentFinish");
//		registerReceiver(finisBroadcastReceiver, finishIntentFilter);
//		IntentFilter photoBackCount = new IntentFilter("photoBackCount");
//		registerReceiver(photoBackCountReceiver, photoBackCount);
//		IntentFilter filter = new IntentFilter("ProgressSpeed");
//		registerReceiver(progressReceiver, filter);
//		IntentFilter Tempfilter = new IntentFilter("TempDate");
//		// IntentFilter Tempfilter = new IntentFilter("ProgressSpeed");
//		registerReceiver(tempReceiver, Tempfilter);
//
//	
//		tempareturePresenter.initAppHead();
//		// tempareturePresenter.initBottomView();
//		tempareturePresenter.initLineChart();
//		tempareturePresenter.initProgress();
//		// for (int i = 0; i < tempArrayList.size(); i++) {
//		// float f = Float.parseFloat(tempArrayList.get(i));
//		// addEntry(0, (float) f);
//		// }
//		startTime();
//		SharedPreferences preferences = getSharedPreferences("ExperimentFinish", Context.MODE_PRIVATE);
//		if (preferences.getBoolean("isFinish", false)) {
//			leftfirstBtn.setEnabled(true);
//			rightfirst_btn.setEnabled(false);
//			time_count.setText("00:00:00");
//		}
//		IntentFilter isPopFinishFilter = new IntentFilter("isPopFinish");
//		registerReceiver(isPopFinishReceiver, isPopFinishFilter);
	}
	
	class TestTask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			dbHelper = new TempDBHelper(TemparetureActivity.this);
			tempArrayList = dbHelper.query();
			// temp2ArrayList = dbHelper.queryTempMode2Name(db);
			temp3ArrayList = dbHelper.queryTempMode3Name();
			Intent intent = getIntent();
			dataSourceListBlock1 = (List<HashMap<String, Object>>) intent.getSerializableExtra("dataSourceListBlock1");
			dataSourceListBlock2 = (List<HashMap<String, Object>>) intent.getSerializableExtra("dataSourceListBlock2");

			activityLayoutTableBeans = (ArrayList<LayoutTableBean>) intent.getSerializableExtra("activityLayoutTableBeans");
			IntentFilter finishIntentFilter = new IntentFilter("ExperimentFinish");
			registerReceiver(finisBroadcastReceiver, finishIntentFilter);
			IntentFilter photoBackCount = new IntentFilter("photoBackCount");
			registerReceiver(photoBackCountReceiver, photoBackCount);
			IntentFilter filter = new IntentFilter("ProgressSpeed");
			registerReceiver(progressReceiver, filter);
			IntentFilter Tempfilter = new IntentFilter("TempDate");
			// IntentFilter Tempfilter = new IntentFilter("ProgressSpeed");
			registerReceiver(tempReceiver, Tempfilter);
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			tempareturePresenter.initAppHead();
			// tempareturePresenter.initBottomView();
			tempareturePresenter.initLineChart();
			tempareturePresenter.initProgress();
			// for (int i = 0; i < tempArrayList.size(); i++) {
			// float f = Float.parseFloat(tempArrayList.get(i));
			// addEntry(0, (float) f);
			// }
			startTime();
			SharedPreferences preferences = getSharedPreferences("ExperimentFinish", Context.MODE_PRIVATE);
			if (preferences.getBoolean("isFinish", false)) {
				leftfirstBtn.setEnabled(true);
				rightfirst_btn.setEnabled(false);
				time_count.setText("00:00:00");
			}
			IntentFilter isPopFinishFilter = new IntentFilter("isPopFinish");
			registerReceiver(isPopFinishReceiver, isPopFinishFilter);
			try {
				messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		overridePendingTransition(0, 0);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	BroadcastReceiver isPopFinishReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			// if (popWindow.isShowing()) {
			// backgroundAlpaha(TemparetureActivity.this, 1f);
			// popWindow.dismiss();
			// popWindow = null;
			// }

		}

	};
	BroadcastReceiver finisBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			try {
				closeProgressDialog();
			} catch (Exception e) {
				// TODO: handle exception
			}
			leftfirstBtn.setEnabled(true);
			rightfirst_btn.setEnabled(false);
			time_count.setText("00:00:00");
			progressBar.setProgress(100);

			// if (isFirstFinish) {
			// if (null == popWindow || !popWindow.isShowing()) {
			// showReturnPop();
			// isFirstFinish = false;
			// }
			// }
		}

	};
	BroadcastReceiver tempReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			startTime();
			SharedPreferences photoBackCountpreferences = getSharedPreferences("photoBackCount", Context.MODE_PRIVATE);
			phototime_count.setText(photoBackCountpreferences.getString("phototime_count", "0"));
			// if (intent.getStringExtra("tempMod1") != null &&
			// !"".equals(intent.getStringExtra("tempMod1"))) {
			// nowTempMod1.setText(intent.getStringExtra("tempMod1"));
			// nowTempMod2.setText(intent.getStringExtra("tempMod2"));
			// nowTempMod3.setText(intent.getStringExtra("tempMod3"));
			//
			// } else {
			SharedPreferences preferences = getSharedPreferences("Temp", Context.MODE_PRIVATE);
			nowTempMod1.setText(preferences.getString("tempMod1", ""));
			nowTempMod2.setText(preferences.getString("tempMod2", ""));
			nowTempMod3.setText(preferences.getString("tempMod3", ""));
			// }

			// xValues.add(xValues.size() + 1 + "");
			LineData data = chart.getData();
			if(data !=null ){
				data.addXValue((data.getXValCount() + 1) + "");
				SharedPreferences Temppreferences = getSharedPreferences("Temp", Context.MODE_PRIVATE);
				if (intent.getStringExtra("temp") == null || "".equals(intent.getStringExtra("temp"))) {

					addEntry(0, Float.parseFloat(Temppreferences.getString("temp", "")));
					addEntry(1, Float.parseFloat(Temppreferences.getString("tempMod3", "")));

				} else {
					addEntry(0, Float.parseFloat(intent.getStringExtra("temp")));
					addEntry(1, Float.parseFloat(Temppreferences.getString("tempMod3", "")));
				}
			}
			
			// addEntry(1, 50);
			// addEntry(0, 30);
		}

	};

	public void backgroundAlpaha(Activity context, float bgAlpha) {
		WindowManager.LayoutParams lp = context.getWindow().getAttributes();
		lp.alpha = bgAlpha;
		context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		context.getWindow().setAttributes(lp);
	}

	private void isDismiss() {
		Intent intent = new Intent("isPopFinish");
		intent.putExtra("isPopFinish", true);
		sendBroadcast(intent);
	}

	// private void showReturnPop() {
	// LayoutInflater inflater = (LayoutInflater) (TemparetureActivity.this)
	// .getSystemService(LAYOUT_INFLATER_SERVICE);
	// View popview = inflater.inflate(R.layout.pop_return_pop, null);
	// popWindow = new PopupWindow(popview, LayoutParams.WRAP_CONTENT,
	// LayoutParams.WRAP_CONTENT);
	//
	// ImageView closeimg = (ImageView) popview.findViewById(R.id.img_closed);
	// TextView txt_returnmain = (TextView) popview
	// .findViewById(R.id.txt_returnmain);
	// popWindow.setTouchable(true);
	// popWindow.setOutsideTouchable(false);
	// TextView txt_returnanylse = (TextView) popview
	// .findViewById(R.id.txt_returnanylse);
	// ColorDrawable dw = new ColorDrawable(0xb0000000);
	// popWindow.setBackgroundDrawable(dw);
	// // popWindow.setBackgroundDrawable(new ColorDrawable());
	// backgroundAlpaha(TemparetureActivity.this, 0.5f);
	// if (popWindow == null || !popWindow.isShowing()) {
	// popWindow.showAtLocation(max_lin, Gravity.CENTER, 0, 0);
	// }
	// if (isStopService) {
	// txt_returnanylse.setVisibility(View.GONE);
	// }
	// closeimg.setOnClickListener(new OnClickListener() {
	//
	// @Override
	// public void onClick(View arg0) {
	// // TODO Auto-generated method stub
	// backgroundAlpaha(TemparetureActivity.this, 1f);
	// popWindow.dismiss();
	// isDismiss();
	// }
	// });
	// txt_returnmain.setOnClickListener(new OnClickListener() {
	//
	// @Override
	// public void onClick(View arg0) {
	// // TODO Auto-generated method stub
	// BaseApplication.getInstance().finishAll();
	// Intent intent = new Intent(TemparetureActivity.this,
	// MainMenuActivity.class);
	// startActivity(intent);
	// overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
	// popWindow.dismiss();
	// }
	// });
	// txt_returnanylse.setOnClickListener(new OnClickListener() {
	//
	// @Override
	// public void onClick(View arg0) {
	// // TODO Auto-generated method stub
	// BaseApplication.getInstance().finishAll();
	// Intent intent = new Intent(TemparetureActivity.this,
	// AnalyzeAmplificationplotActivity.class);
	// startActivity(intent);
	// overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
	// popWindow.dismiss();
	// }
	// });
	// }

	// 添加进去一个坐标点
	private void addEntry(int i, float f) {

		LineData data = chart.getData();

		// 每一个LineDataSet代表一条线，每张统计图表可以同时存在若干个统计折线，这些折线像数组一样从0开始下标。
		// 本例只有一个，那么就是第0条折线

		LineDataSet set = data.getDataSetByIndex(i);

		// 如果该统计折线图还没有数据集，则创建一条出来，如果有则跳过此处代码。
		if (set == null) {
			set = createLineDataSet();
			data.addDataSet(set);
		}

		// 先添加一个x坐标轴的值
		// 因为是从0开始，data.getXValCount()每次返回的总是全部x坐标轴上总数量，所以不必多此一举的加1
		// data.addXValue((data.getXValCount()) + "");

		// // 生成随机测试数
		// float f f;

		// set.getEntryCount()获得的是所有统计图表上的数据点总量，
		// 如从0开始一样的数组下标，那么不必多次一举的加1
		Entry entry = new Entry(f, set.getEntryCount());

		// 往linedata里面添加点。注意：addentry的第二个参数即代表折线的下标索引。
		// 因为本例只有一个统计折线，那么就是第一个，其下标为0.
		// 如果同一张统计图表中存在若干条统计折线，那么必须分清是针对哪一条（依据下标索引）统计折线添加。
		data.addEntry(entry, i);

		// 像ListView那样的通知数据更新
		chart.notifyDataSetChanged();

		// 当前统计图表中最多在x轴坐标线上显示的总量
		// chart.setVisibleXRangeMaximum(5);

		// y坐标轴线最大值
		// mChart.setVisibleYRange(30, AxisDependency.LEFT);

		// 将坐标移动到最新
		// 此代码将刷新图表的绘图
		chart.moveViewToX(0);

		// mChart.moveViewTo(data.getXValCount()-7, 55f,
		// AxisDependency.LEFT);
	}

	// 初始化数据集，添加一条统计折线，可以简单的理解是初始化y坐标轴线上点的表征
	private LineDataSet createLineDataSet() {

		LineDataSet set = new LineDataSet(null, "");
		set.setAxisDependency(AxisDependency.LEFT);

		// 折线的颜色
		set.setColor(ColorTemplate.getHoloBlue());

		set.setCircleColor(Color.RED);
		set.setLineWidth(2f);
		set.setCircleSize(0f);

		set.setHighLightColor(Color.RED);
		set.setValueTextColor(Color.RED);
		set.setValueTextSize(0f);
		set.setDrawValues(true);
		return set;

	};

	BroadcastReceiver photoBackCountReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub

		}
	};

	private void startTime() {
		SharedPreferences preferences = getSharedPreferences("Time", Context.MODE_PRIVATE);

		start_time.setText(preferences.getString("startTime", null));
		end_time.setText(preferences.getString("endTime", null));
	}

	@Override
	public void onClick(View v) {
		// switch (v.getId()) {
		// case R.id.temparetureTv1:
		//
		// if (tempareture1) {
		// tempareture1 = !tempareture1;
		// dataSets.remove(dataSet1);
		// // 构建一个LineData 将dataSets放入
		// LineData lineData = new LineData(xValues, dataSets);
		//
		// // mLineChart2.setBackgroundColor(Color.BLUE);
		// chart.setGridBackgroundColor(Color.WHITE);
		// // 将数据插入
		// chart.setData(lineData);
		// Legend mLegend = chart.getLegend(); // 设置比例图标示，就是那个一组y的value的
		// mLegend.setXEntrySpace(7f);
		// mLegend.setYEntrySpace(5f);
		// chart.animateX(0);
		//
		// } else {
		// tempareture1 = !tempareture1;
		// dataSets.add(dataSet1);
		//
		// // 构建一个LineData 将dataSets放入
		// LineData lineData = new LineData(xValues, dataSets);
		//
		// // mLineChart2.setBackgroundColor(Color.BLUE);
		// chart.setGridBackgroundColor(Color.WHITE);
		// // 将数据插入
		// chart.setData(lineData);
		// Legend mLegend = chart.getLegend(); // 设置比例图标示，就是那个一组y的value的
		// mLegend.setXEntrySpace(7f);
		// mLegend.setYEntrySpace(5f);
		// chart.animateX(0);
		// }
		//
		// break;
		// case R.id.temparetureTv2:
		// if (tempareture2) {
		// tempareture2 = !tempareture2;
		// dataSets.remove(dataSet2);
		//
		// // 构建一个LineData 将dataSets放入
		// LineData lineData = new LineData(xValues, dataSets);
		//
		// // mLineChart2.setBackgroundColor(Color.BLUE);
		// chart.setGridBackgroundColor(Color.WHITE);
		// // 将数据插入
		// chart.setData(lineData);
		// Legend mLegend = chart.getLegend(); // 设置比例图标示，就是那个一组y的value的
		// mLegend.setXEntrySpace(7f);
		// mLegend.setYEntrySpace(5f);
		// chart.animateX(0);
		//
		// } else {
		// tempareture2 = !tempareture2;
		// dataSets.add(dataSet2);
		//
		// // 构建一个LineData 将dataSets放入
		// LineData lineData = new LineData(xValues, dataSets);
		//
		// // mLineChart2.setBackgroundColor(Color.BLUE);
		// chart.setGridBackgroundColor(Color.WHITE);
		// // 将数据插入
		// chart.setData(lineData);
		// Legend mLegend = chart.getLegend(); // 设置比例图标示，就是那个一组y的value的
		// mLegend.setXEntrySpace(7f);
		// mLegend.setYEntrySpace(5f);
		// chart.animateX(0);
		// }
		// break;
		//
		// default:
		// break;
		// }

	}

	@Override
	public void initAppHead() {
		setActionBar();
		setSystemTime();
	}

	@Override
	public void initBottomView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initLineChart() {

		XAxis xAxis = chart.getXAxis();

		xValues = new ArrayList<String>();
		for (int i = 0; i < tempArrayList.size() + 5; i++) {
			xValues.add(i + "");
		}

		// 设置X轴的文字在底部
		xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

		// 设置描述文字
		chart.setDescription("");

		// 模拟第二组组y轴数据(存放y轴数据的是一个Entry的ArrayList) 他是构建LineDataSet的参数之一

		ArrayList<Entry> yValue1 = new ArrayList<Entry>();
		ArrayList<Entry> yValue2 = new ArrayList<Entry>();
		ArrayList<Entry> yValue3 = new ArrayList<Entry>();
		for (int i = 0; i < tempArrayList.size(); i++) {
			yValue1.add(new Entry(Float.parseFloat(tempArrayList.get(i)), i));
		}
		// for (int i = 0; i < temp2ArrayList.size(); i++) {
		// yValue2.add(new Entry(Float.parseFloat(temp2ArrayList.get(i)), i));
		// }
		for (int i = 0; i < temp3ArrayList.size(); i++) {
			yValue3.add(new Entry(Float.parseFloat(temp3ArrayList.get(i)), i));
		}

		// 构建一个LineDataSet 代表一组Y轴数据 （比如不同的彩票： 七星彩 双色球）

		dataSet2 = new LineDataSet(yValue1, "");
		dataSet1 = new LineDataSet(yValue3, "");
		dataSet1.setCircleSize(0);
		dataSet1.setValueTextSize(0);
		dataSet1.setLineWidth(2);
		dataSet1.setColor(Color.BLUE);// 设置颜色
		dataSet2.setCircleSize(0);
		dataSet2.setValueTextSize(0);
		dataSet2.setLineWidth(3);
		// dataSet2.setDrawCubic(true);// 平滑显示
		// dataSet2.setCubicIntensity(0.1f);// 设置平滑度
		dataSet2.setColor(Color.RED);// 设置颜色
		dataSets = new ArrayList<LineDataSet>();

		// 将数据加入dataSets
		dataSets.add(dataSet2);
		dataSets.add(dataSet1);
		// 构建一个LineData 将dataSets放入
		LineData lineData = new LineData(xValues, dataSets);
		// mLineChart2.setBackgroundColor(Color.BLUE);
		chart.setGridBackgroundColor(Color.WHITE);
		// 将数据插入
		chart.setData(lineData);
		Legend mLegend = chart.getLegend(); // 设置比例图标示，就是那个一组y的value的
		mLegend.setXEntrySpace(7f);
		mLegend.setYEntrySpace(5f);
		mLegend.setForm(LegendForm.LINE);// 样式
		mLegend.setFormSize(0f);// 字体
		mLegend.setTextColor(Color.WHITE);// 颜色

		chart.animateX(0); // 立即执行的动画,x轴
		System.gc();
	}

	@Override
	public void initSpinnerView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initProgress() {
		// new Thread(runnable).start();

	}

	BroadcastReceiver progressReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String progressString = intent.getStringExtra("Progress");
			progressBar.setProgress(Integer.parseInt(progressString));
			String nowString = intent.getStringExtra("NowTime");
			time_count.setText(nowString);
		}

	};

	// 设置actionBar
	public void setActionBar() {

		// Button leftsecondBtn, leftThird_btn, leftForth_btn, leftFifth_btn,
		// leftSixth_btn, leftSevenTh_btn, leftEighth_btn, leftNineth_btn,
		// rightfirst_btn;
		// LinearLayout rightline;
		// TextView title;
		leftfirstBtn = BindView(R.id.leftfirst_btn);
		leftsecondBtn = BindView(R.id.leftsecond_btn);
		leftThird_btn = BindView(R.id.leftThird_btn);
		leftForth_btn = BindView(R.id.leftForth_btn);

		leftFifth_btn = BindView(R.id.leftFifth_btn);
		leftSixth_btn = BindView(R.id.leftSixth_btn);
		leftSevenTh_btn = BindView(R.id.leftSevenTh_btn);
		leftEighth_btn = BindView(R.id.leftEighth_btn);
		leftNineth_btn = BindView(R.id.leftNineth_btn);
		leftTenth_btn = BindView(R.id.leftTenth_btn);

		rightfirst_btn = BindView(R.id.rightfirst_btn);
		rightline = BindView(R.id.rightline);
		// title = BindView(R.id.title);

		leftSixth_btn.setVisibility(View.GONE);
		leftSevenTh_btn.setVisibility(View.GONE);
		leftEighth_btn.setVisibility(View.GONE);
		leftNineth_btn.setVisibility(View.GONE);
		leftTenth_btn.setVisibility(View.GONE);

		leftfirstBtn.setVisibility(View.VISIBLE);
		leftfirstBtn.setText(R.string.item_actionbar_home);
		leftsecondBtn.setVisibility(View.VISIBLE);
		leftsecondBtn.setText(getString(R.string.Run_Amplification));
		leftThird_btn.setSelected(true);
		leftThird_btn.setVisibility(View.VISIBLE);
		leftThird_btn.setText(getString(R.string.Run_Teperature1));
		leftForth_btn.setVisibility(View.VISIBLE);
		leftForth_btn.setText(getString(R.string.Run_Method));
		SharedPreferences preferences = getSharedPreferences("Delet", Context.MODE_PRIVATE);
		if (preferences.getBoolean("Delet", false)) {
			leftFifth_btn.setVisibility(View.VISIBLE);
			leftFifth_btn.setText(getString(R.string.Run_MeltCurve));
		} else {
			leftFifth_btn.setVisibility(View.GONE);

		}
		rightfirst_btn.setVisibility(View.VISIBLE);
		rightfirst_btn.setText(getString(R.string.Run_Stop));
		rightline.setVisibility(View.VISIBLE);
		// title.setText("Amplifucation Plot");
		leftfirstBtn.setEnabled(false);
		leftfirstBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (!menuboo) {
				// showMenuPopupWindow(v);
				// menuboo = !menuboo;
				// } else {
				// menuPopupWindow.dismiss();
				// menuboo = !menuboo;
				// }
				BaseApplication.getInstance().finishAll();
				Intent intent = new Intent(TemparetureActivity.this, MainMenuActivity.class);
				startActivity(intent);
				overridePendingTransition(0, 0);
				// stopService(serviceintent);
			}

		});

		leftsecondBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				pd = ProgressDialog.show(TemparetureActivity.this, "", "loading...", true, false);
//
//				new Thread() {
//
//					public void run() {
//						try {

							Intent intent = new Intent(TemparetureActivity.this, AmplifucationActivity.class);
							startActivity(intent);
							intent.putExtra("dataSourceListBlock1", (Serializable) dataSourceListBlock1);
							intent.putExtra("dataSourceListBlock2", (Serializable) dataSourceListBlock2);
							intent.putExtra("activityLayoutTableBeans", (Serializable) activityLayoutTableBeans);
							overridePendingTransition(0, 0);
							finish();
//						} finally {
//							messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE);
//						}
//					}
//				}.start();

			}
		});
		leftThird_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				pd = ProgressDialog.show(TemparetureActivity.this, "", "loading...", true, false);
//
//				new Thread() {
//
//					public void run() {
//						try {
//							Intent intent = new Intent(TemparetureActivity.this, TemparetureActivity.class);
//							intent.putExtra("dataSourceListBlock1", (Serializable) dataSourceListBlock1);
//							intent.putExtra("dataSourceListBlock2", (Serializable) dataSourceListBlock2);
//							intent.putExtra("activityLayoutTableBeans", (Serializable) activityLayoutTableBeans);
//							startActivity(intent);
//							overridePendingTransition(0, 0);
//
//						} finally {
//							messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE);
//						}
//					}
//				}.start();

			}
		});
		leftForth_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

//				pd = ProgressDialog.show(TemparetureActivity.this, "", "loading...", true, false);
//
//				new Thread() {
//
//					public void run() {
//						try {

							Intent intent = new Intent(TemparetureActivity.this, RunMethodActivity.class);
							intent.putExtra("dataSourceListBlock1", (Serializable) dataSourceListBlock1);
							intent.putExtra("dataSourceListBlock2", (Serializable) dataSourceListBlock2);
							intent.putExtra("activityLayoutTableBeans", (Serializable) activityLayoutTableBeans);
							startActivity(intent);
							overridePendingTransition(0, 0);
							finish();

//						} finally {
//							messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE);
//						}
//					}
//				}.start();

			}
		});
		leftFifth_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
//				pd = ProgressDialog.show(TemparetureActivity.this, "", "loading...", true, false);
//
//				new Thread() {
//
//					public void run() {
//						try {

							Intent intent = new Intent(TemparetureActivity.this, AmplifucationTwoActivity.class);
							intent.putExtra("dataSourceListBlock1", (Serializable) dataSourceListBlock1);
							intent.putExtra("dataSourceListBlock2", (Serializable) dataSourceListBlock2);
							intent.putExtra("activityLayoutTableBeans", (Serializable) activityLayoutTableBeans);
							startActivity(intent);
							overridePendingTransition(0, 0);
							finish();

//						} finally {
//							messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE);
//						}
//					}
//				}.start();
//				// TODO Auto-generated method stub

			}
		});
		rightfirst_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final AllShengDialog allShengDialog = new AllShengDialog(TemparetureActivity.this, "", "确定要停止实验吗", "OK",
						"NO");
				allShengDialog.show();
				allShengDialog.setClicklistener(new ClickListenerInterface() {

					@Override
					public void OK() {
						// TODO Auto-generated method stub
						isStopService = true;
						Intent intent = new Intent("StopService");
						sendBroadcast(intent);
						showProgressDialog("正在降温....", true);
						allShengDialog.dismiss();

					}

					@Override
					public void Cancle() {
						// TODO Auto-generated method stub
						allShengDialog.dismiss();

					}
				});
			}
		});

	}

	private Handler messageListener = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.arg1) {
			case TASK_LOOP_COMPLETE:
				pd.dismiss();
				break;

			}
		}
	};

	// 获取当前时间的方法
	public void setSystemTime() {

		TextView date, fileName;
		date = BindView(R.id.date);
		fileName = BindView(R.id.file_name);

		// 获取系统时间
		SimpleDateFormat sdfDate = new SimpleDateFormat("MM dd,yyyy");
		date.setText(sdfDate.format(new Date()));

		// 文件路径置空
		fileName.setText("");

	}

	private boolean isForeground() {
		if (TemparetureActivity.this == null) {
			return false;
		}

		ActivityManager am = (ActivityManager) TemparetureActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> list = am.getRunningTasks(1);
		if (list != null && list.size() > 0) {
			ComponentName cpn = list.get(0).topActivity;
			if ("TemparetureActivity".equals(cpn.getClassName())) {
				return true;
			}
		}

		return false;
	}

}
